﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Vit.Web.UI
{
    public class MvcControl : Control
    {
        protected override void Render(HtmlTextWriter writer)
        {
            if (string.IsNullOrEmpty(Url))
                Url = HttpContext.Current.Request.Url.AbsolutePath;
            if (InDesignMode)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.Attributes.Add("class", "designControl");
                div.InnerHtml = Url;
                this.Controls.Add(div);
                base.Render(writer);
            }
            else
            {
                HttpContext.Current.RewritePath(Url);
                IHttpHandler handler = new MvcHttpHandler();
                handler.ProcessRequest(HttpContext.Current);
            }
        }

        public string Url { get; set; }

        public bool InDesignMode
        {
            get
            {
                return this.Page.Request.Path.EndsWith(".design.aspx");
            }
        }
    }
}
